package com.gm.wholesale.module.main.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.gm.commonlib.base.baseAdapter.BaseCommAdapter;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.LeftMenuType;
import com.gm.wholesale.constant.MenuData;
import com.gm.wholesale.module.config.ConfigData;
import com.gm.wholesale.module.main.view.MenuItemView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 主界面侧滑 菜单view
 */
public class LeftMenuFragment extends BaseFragment {
    private OnMenuItemClickListener listener;

    public OnMenuItemClickListener getListener() {
        return listener;
    }

    public void setListener(OnMenuItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnMenuItemClickListener{
        void onMenuItemClickListener(MenuData menuData);
    }
    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        View bodyView= LayoutInflater.from(getActivity()).inflate(R.layout.view_menu_body,parent);
        bodyView.findViewById(R.id.settingLin).setOnClickListener(view -> {
            if (listener!=null){
                listener.onMenuItemClickListener(MenuData.SETTING);
            }
        });
        GridView gridView=bodyView.findViewById(R.id.gridView);
        gridView.setAdapter(new BaseCommAdapter<>(getActivity(), getMenuData()) {
            @Override
            public View getView(MenuData data, View convertView, int postion) {
                MenuItemView menuItemView = new MenuItemView(getActivity());
                menuItemView.showMenu(data.getImage(), data.getName());
                menuItemView.setOnClickListener(view -> {
                    if (listener!=null){
                        listener.onMenuItemClickListener(data);
                    }
                });
                return menuItemView;
            }
        });
    }
    public static List<MenuData> getMenuData() {
        List<MenuData> enumList=Arrays.asList(MenuData.values());
        List<MenuData> newData=new ArrayList<>();
        for (MenuData menuData:enumList){
            if (menuData.getType()== LeftMenuType.RECEIV_MONEY){
                if (!ConfigData.getConfigData().hasReceivablePermission()){
                   continue;
                }
            }
            if (menuData.getType()== LeftMenuType.SALES_REPORT){
                if (!ConfigData.getConfigData().hasDataReportPermission()){
                    continue;
                }
            }
            newData.add(menuData);
        }
        return newData;
    }

}
